<?php    
class ControllerLocalisationBank extends Controller { 
	private $error = array();
  
  	public function index() {
		$this->load->language('localisation/bank');
		
		$this->document->setTitle($this->language->get('heading_title'));
		 
		$this->load->model('localisation/bank');
		
    	$this->getList();
  	}
  
  	public function insert() {
		$this->load->language('localisation/bank');

    	$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('localisation/bank');
			
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_localisation_bank->addBank($this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';
			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=localisation/bank&token=' . $this->session->data['token'] . $url);
		}
    
    	$this->getForm();
  	} 
   
  	public function update() {
		$this->load->language('localisation/bank');

    	$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('localisation/bank');
		
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_localisation_bank->editBank($this->request->get['bank_id'], $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=localisation/bank&token=' . $this->session->data['token'] . $url);
		}
    
    	$this->getForm();
  	}   

  	public function delete() {
		$this->load->language('localisation/bank');

    	$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('localisation/bank');
			
    	if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $bank_id) {
				$this->model_localisation_bank->deleteBank($bank_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');
			
						
			$this->redirect(HTTPS_SERVER . 'index.php?route=localisation/bank&token=' . $this->session->data['token']);
    	}
	
    	$this->getList();
  	}  
  	
  	public function connect() {
		$this->load->language('localisation/bank');

    	$this->document->setTitle($this->language->get('heading_title'));
    	
    	$this->data['heading_title'] = $this->language->get('heading_title');
    	
    	$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');
    	
  	 	if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}    	
		
		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=localisation/bank&token=' . $this->session->data['token'];
	
		$this->template = 'localisation/bank_connect.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
  	}   	
    
  	private function getList() {
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'bank_name';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		$url = '';
			
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

  		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=localisation/bank&token=' . $this->session->data['token'] . $url,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
							
		$this->data['insert'] = HTTPS_SERVER . 'index.php?route=localisation/bank/insert&token=' . $this->session->data['token'] . $url;
		$this->data['delete'] = HTTPS_SERVER . 'index.php?route=localisation/bank/delete&token=' . $this->session->data['token'] . $url;	

		$this->data['banks'] = array();

		$data = array(
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		
		$localisation_bank_total = $this->model_localisation_bank->getTotalBanks();
	
		$results = $this->model_localisation_bank->getBanks($data);
 
		$this->data['localisation_banks'] = array();
		
    	foreach ($results as $result) {
			$action = array();
			
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => HTTPS_SERVER . 'index.php?route=localisation/bank/update&token=' . $this->session->data['token'] . '&bank_id=' . $result['bank_id'] . $url
			);
						
			$this->data['localisation_banks'][] = array(
				'bank_id'			=> $result['bank_id'],
				'currency'			=> $result['currency'],
				'bank_name'			=> $result['bank_name'],
				'bank_account'		=> $result['bank_account'],
				'bank_account_name'	=> $result['bank_account_name'],
				'sort_order'		=> $result['sort_order'],
				'fee'				=> $result['fee'],
				'status'			=> ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),			
				'date_added'		=> $result['date_added'],
				'selected'			=> isset($this->request->post['selected']) && in_array($result['bank_id'], $this->request->post['selected']),
				'action'			=> $action
			);
		}	
	
		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_bank_name'] = $this->language->get('column_bank_name');
		$this->data['column_bank_account'] = $this->language->get('column_bank_account');
		$this->data['column_bank_account_name'] = $this->language->get('column_bank_account_name');
		$this->data['column_currency'] = $this->language->get('column_currency');
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
		$this->data['column_fee'] = $this->language->get('column_fee');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_date_added'] = $this->language->get('column_date_added');
		$this->data['column_action'] = $this->language->get('column_action');		
		
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		$this->data['sort_bank_name'] = HTTPS_SERVER . 'index.php?route=localisation/bank&token=' . $this->session->data['token'] . '&sort=bank_name' . $url;
		$this->data['sort_sort_order'] = HTTPS_SERVER . 'index.php?route=localisation/bank&token=' . $this->session->data['token'] . '&sort=sort_order' . $url;
		
		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $localisation_bank_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = HTTPS_SERVER . 'index.php?route=localisation/bank&token=' . $this->session->data['token'] . $url . '&page={page}';
			
		$this->data['pagination'] = $pagination->render();

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;
		
		$this->template = 'localisation/bank_list.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
  
  	private function getForm() {
    	$this->data['heading_title'] = $this->language->get('heading_title');

    	$this->data['text_enabled'] = $this->language->get('text_enabled');
    	$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_default'] = $this->language->get('text_default');
    	$this->data['text_image_manager'] = $this->language->get('text_image_manager');
		
		$this->data['entry_bank_name'] = $this->language->get('entry_bank_name');
		$this->data['entry_bank_account'] = $this->language->get('entry_bank_account');
		$this->data['entry_bank_account_name'] = $this->language->get('entry_bank_account_name');
		$this->data['entry_currency'] = $this->language->get('entry_currency');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_fee'] = $this->language->get('entry_fee');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_date_added'] = $this->language->get('entry_date_added');
		$this->data['entry_keyword'] = $this->language->get('entry_keyword');
    	$this->data['entry_image'] = $this->language->get('entry_image');
  
    	$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');
	  
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

 		if (isset($this->error['bank_name'])) {
			$this->data['error_bank_name'] = $this->error['bank_name'];
		} else {
			$this->data['error_bank_name'] = '';
		}
		
  	 	if (isset($this->error['bank_account'])) {
			$this->data['error_bank_account'] = $this->error['bank_account'];
		} else {
			$this->data['error_bank_account'] = '';
		}

  	 	if (isset($this->error['bank_account_name'])) {
			$this->data['error_bank_account_name'] = $this->error['bank_account_name'];
		} else {
			$this->data['error_bank_account_name'] = '';
		}		
		    
		
  		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=localisation/bank&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
							
		if (!isset($this->request->get['bank_id'])) {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=localisation/bank/insert&token=' . $this->session->data['token'];
		} else {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=localisation/bank/update&token=' . $this->session->data['token'] . '&bank_id=' . $this->request->get['bank_id'] ;
		}
		
		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=localisation/bank&token=' . $this->session->data['token'];

		$this->data['token'] = $this->session->data['token'];
		
    	if (isset($this->request->get['bank_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$bank_info = $this->model_localisation_bank->getBank($this->request->get['bank_id']);
    	}

    	if (isset($this->request->post['bank_name'])) {
      		$this->data['bank_name'] = $this->request->post['bank_name'];
    	} elseif (isset($bank_info)) {
			$this->data['bank_name'] = $bank_info['bank_name'];
		} else {	
      		$this->data['bank_name'] = '';
    	}
    	
  	    if (isset($this->request->post['bank_account'])) {
      		$this->data['bank_account'] = $this->request->post['bank_account'];
    	} elseif (isset($bank_info)) {
			$this->data['bank_account'] = $bank_info['bank_account'];
		} else {	
      		$this->data['bank_account'] = '';
    	}

  	    if (isset($this->request->post['bank_account_name'])) {
      		$this->data['bank_account_name'] = $this->request->post['bank_account_name'];
    	} elseif (isset($bank_info)) {
			$this->data['bank_account_name'] = $bank_info['bank_account_name'];
		} else {	
      		$this->data['bank_account_name'] = '';
    	}    	
    	
		$this->load->model('localisation/currency');
		
		$this->data['currencies'] = $this->model_localisation_currency->getCurrencies();

  	    if (isset($this->request->post['currency_id'])) {
      		$this->data['currency_id'] = $this->request->post['currency_id'];
    	} elseif (isset($bank_info)) {
			$this->data['currency_id'] = $bank_info['currency_id'];
		} else {	
      		$this->data['currency_id'] = '';
    	}		
		if (isset($this->request->post['sort_order'])) {
      		$this->data['sort_order'] = $this->request->post['sort_order'];
    	} elseif (isset($bank_info)) {
			$this->data['sort_order'] = $bank_info['sort_order'];
		} else {	
      		$this->data['sort_order'] = '';
    	}  	
  		if (isset($this->request->post['fee'])) {
      		$this->data['fee'] = $this->request->post['fee'];
    	} elseif (isset($bank_info)) {
			$this->data['fee'] = $bank_info['fee'];
		} else {	
      		$this->data['fee'] = '';
    	}
    	
		if (isset($this->request->post['status'])) {
			$this->data['status'] = $this->request->post['status'];
    	} elseif (isset($bank_info)) {
			$this->data['status'] = $bank_info['status'];			
		} else {
			$this->data['status'] = $this->config->get('status');
		}		

		$this->template = 'localisation/bank_form.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}  
	 
  	private function validateForm() {
    	if (!$this->user->hasPermission('modify', 'localisation/bank')) {
      		$this->error['warning'] = $this->language->get('error_permission');
    	}
		
  	    if ((strlen(utf8_decode($this->request->post['bank_name'])) < 3) || (strlen(utf8_decode($this->request->post['bank_name'])) > 96)) {
      		$this->error['bank_name'] = $this->language->get('error_bank_name');
    	}
    	
  	  	if ((strlen(utf8_decode($this->request->post['bank_account'])) < 3) || (strlen(utf8_decode($this->request->post['bank_account'])) > 96)) {
      		$this->error['bank_account'] = $this->language->get('error_bank_account');
    	}    	

  	  	if ((strlen(utf8_decode($this->request->post['bank_account_name'])) < 3) || (strlen(utf8_decode($this->request->post['bank_account_name'])) > 96)) {
      		$this->error['bank_account_name'] = $this->language->get('error_bank_account_name');
    	}
    	    	
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
  	}    

  	private function validateDelete() {
    	if (!$this->user->hasPermission('modify', 'localisation/bank')) {
			$this->error['warning'] = $this->language->get('error_permission');
    	}
    	
//  		$this->load->model('sale/order');
//		
//		foreach ($this->request->post['selected'] as $bank_id) {
//			
//			$bank_total = $this->model_sale_order->getBankTotalByBankId($bank_id);
//		
//			if ($bank_total) {
//	  			$this->error['warning'] = sprintf($this->language->get('error_order'), $bank_total);	
//			}  
//	  	}    	
				
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}  
  	}
}
?>